<template>
    <view class="main">
        <button type="default" @click="login()">微信登录</button>
        <button type="default" @click="pay()">微信支付</button>
    </view>
</template>

<script>
    import api from "../../api/index.js"
    export default {
        data() {
            return {
                openid: '',

            }
        },
        computed: {
            isLogin() {
                return api.getToken();
            },
            userInfo() {
                return api.getToken()
            }
        },
        methods: {
            login() {

                wx.login({
                    success:res => {
                        console.log("code", res)
                        if (res.code) {
                            wx.getUserInfo({
                                success: info => {
                                    console.log("info", info)
                                    let rawData = info['rawData'];
                                    let signature = info['signature'];
                                    let encryptData = info['encryptData'];
                                    let encryptedData = info['encryptedData'];
                                    let iv = info['iv'];

                                    //发起网络请求
                                    wx.request({
                                        url: 'http://test.dtapi.com/index.php/index/wxLogin',
                                        data: {
                                            code: res.code,
                                            rawData: rawData,
                                            signature: signature,
                                            encryptData: encryptData,
                                            iv: iv,
                                            encryptedData: encryptedData,
                                        },
                                        success: res => {
                                            console.log("微信登录结果", res);
                                            if(res.data.status == 'ok'){
                                                // 存储openid
                                                this.openid = res.data.data.openId
                                                console.log(".openid", this.openid);
                                                uni.showToast({
                                                    title: '登录成功'
                                                });
                                            }else{

                                            }
                                        }
                                    })
                                }
                            })

                        } else {
                            console.log('登录失败！' + res.errMsg)
                        }
                    }
                })
            },
            pay() {
                if(this.openid == ''){
                    uni.showToast({
                        title: '请先登录'
                    });
                    return;
                }
                // 1 后台创建订单
                api.post(
                    'http://test.dtapi.com/index.php/index/createOrder', {
                        title: '商品名称',
                        money: 1,
                        openid: this.openid
                    },
                    res => {
                        //订单信息
                        console.log('res', res);
                        //2 客户端请求支付
                        wx.requestPayment({
                            timeStamp: res.data.timeStamp,
                            nonceStr: res.data.nonceStr,
                            package: res.data.package,
                            signType: res.data.signType, // MD5或RSA（根据返回使用）
                            paySign: res.data.paySign,
                            success:res => {
                                console.log("微信支付",res)
                            },
                            fail:res => {
                                console.log("微信支付失败",res)
                            }
                        })
                    },
                    err => {
                        console.log("err", err)
                    }
                )
            }
        }
    }
</script>

<style>
    .main {
        padding-top: 10vh;
    }
</style>